-module(try_test).
-export([demo1/0, demo2/0, demo3/0]).

generate_execption(1) -> a;
generate_execption(2) -> throw(a);
generate_execption(3) -> exit(a);
generate_execption(4) -> {'EXIT', a};
generate_execption(5) -> error(a).

demo1() -> [catcher(I) || I <- [1,2,3,4,5]].

catcher(N) ->
  try generate_execption(N) of
    Val -> { N, normal, Val}
  catch
    throw:X -> {N, caught, thrown, X};
    exit:X -> {N, caught, exited, X};
    error:X -> {N, caught, error, X}
  end.

demo2() ->
  [{I, (catch generate_execption(I))} || I <- [1,2,3,4,5]].

demo3() ->
  try generate_execption(5)
  catch
    error:X ->
      {X, erlang:get_stacktrace()}
  end.
